package org.zqh.scaleclient;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScaleTask implements Runnable{

    String url;
    // Endpoint。
    String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    String accessKeyId = "LTAI4G9RCCiaTbusrrJhgwYm";
    String accessKeySecret = "89wPeC5fp1jpFgTfPSON35dZcEpDjh";

    OSS ossClient=null;

    public ScaleTask()
    {
        // 创建OSSClient实例。
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }



    @Override
    public void run() {
        LogKit.info("ScaleTask run...");
        LogKit.info("count="+Db.queryInt("select count(*) from v_bdsync"));
        url= PropKit.get("scale_server_url");
        List<Record> recordList = Db.find("select * from v_bdsync");

        for(Record r:recordList)
        {
            Map<String,String> postMap = new HashMap<>();
            String bdcode = r.get("VBDCODE","");
            String dbcode = r.get("VDBCODE","");
            //String intime = r.get("DINTIME","");
            postMap.put("bdcode",bdcode);
            postMap.put("fromname",r.get("VFROMNAME",""));
            postMap.put("toname",r.get("VTONAME",""));
            postMap.put("ingby",r.get("VINGBY",""));
            postMap.put("carno",r.get("VCARNO",""));
            postMap.put("dbcode",dbcode);
            postMap.put("dbname",r.get("VDBNAME",""));
            postMap.put("maozhong",""+r.getInt("NMAOZHONG"));
            postMap.put("jingzhong",""+r.getInt("NJINGZHONG"));
            postMap.put("pizhong",""+r.getInt("NPIZHONG"));
            postMap.put("bdtype",r.get("VBDTYPE",""));
            postMap.put("delflag",r.get("VDELFLAG",""));
            postMap.put("note",r.get("VNOTE",""));
            postMap.put("erpno",r.get("VERPNO",""));
            postMap.put("erphid",r.get("NERPHID",""));
            postMap.put("erpbid",r.get("NERPBID",""));
            postMap.put("pkstockorg",r.get("PKSTOCKORG",""));
            postMap.put("sendnc",r.get("VSENDNC",""));
            postMap.put("intime",r.get("DINTIME",""));
            postMap.put("outtime",r.get("DOUTTIME",""));
            postMap.put("matname",r.get("VMATNAME",""));
            postMap.put("hw1",r.get("VHW1",""));
            postMap.put("hw2",r.get("VHW2",""));
            try {
                int dbcodeLen = dbcode.length();
                String syear = bdcode.substring(dbcodeLen+2,dbcodeLen+6);
                String smonth = bdcode.substring(dbcodeLen+6,dbcodeLen+8);
                String sday = bdcode.substring(dbcodeLen+8,dbcodeLen+10);
                String filepath = "db/"+dbcode+"/"+syear+"/"+smonth+"/"+sday+"/";
                String filename_in_3 = uploadPicToYun(r.get("INPICFILE3"), filepath+bdcode + "_in_3.jpg");
                String filename_in_4 = uploadPicToYun(r.get("INPICFILE4"), filepath+bdcode + "_in_4.jpg");
                String filename_out_3 = uploadPicToYun(r.get("OUTPICFILE3"), filepath+bdcode + "_out_3.jpg");
                String filename_out_4 = uploadPicToYun(r.get("OUTPICFILE4"), filepath+bdcode + "_out_4.jpg");
                postMap.put("inpicfile3",filename_in_3);
                postMap.put("inpicfile4",filename_in_4);
                postMap.put("outpicfile3",filename_out_3);
                postMap.put("outpicfile4",filename_out_4);
            }catch (Exception e)
            {
                LogKit.error("uploadPicToYun",e);
            }

            String returnStr = HttpKit.post(url,postMap,null);

            LogKit.info("vbdcode="+bdcode+" result="+returnStr);
            if(returnStr.equalsIgnoreCase("ok")) {
                Db.update("update t_bangdan set vsendserver='1' where vbdcode='" + bdcode + "'");
            }else
            {
                Db.update("update t_bangdan set vsendserver='4' where vbdcode='" + bdcode + "'");
            }


        }

    }

    private String uploadPicToYun(String filePathName,String fileName)
    {
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest("zhonghuifile", fileName, new File(filePathName));

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        // ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);
        // putObjectRequest.setMetadata(metadata);

        // 上传文件。
        if(null==ossClient) {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        }
        PutObjectResult por = ossClient.putObject(putObjectRequest);

        // 关闭OSSClient。
        //ossClient.shutdown();

        return "https://zhonghuifile.oss-cn-qingdao.aliyuncs.com/"+fileName;
    }



}
